<template>
  <comcard>
    <div slot="title_name">商品列表</div>
    <div slot="body_name">
      <el-row>
        <el-form :inline="true" :model="fromObj">
          <el-form-item label="商品名称">
            <el-input type="text" v-model="fromObj.name"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-input type="text" v-model="fromObj.category"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="query">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-row>

      <template>
        <el-table :data="dataSouce" style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="商品id:">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="商品名称:">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="所属分类:">
                  <span>{{ props.row.category }}</span>
                </el-form-item>
                <el-form-item label="商品价格:">
                  <span>{{ props.row.price }}</span>
                </el-form-item>
                <el-form-item label="商品图片:"> </el-form-item>
                <el-form-item label="创建时间:">
                  <span>{{ props.row.ctime | timeMF }}</span>
                </el-form-item>
                <el-form-item label="商品评价">
                  <span>{{ props.row.rating }}</span>
                </el-form-item>
                <el-form-item label="商品销量">
                  <span>{{ props.row.sellCount }}</span>
                </el-form-item>
                <el-form-item label="商品描述">
                  <span>{{ props.row.goodsDesc }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="id"> </el-table-column>
          <el-table-column label="所属分类" prop="name"> </el-table-column>
          <el-table-column label="价格" prop="price"> </el-table-column>
          <el-table-column label="商品图片">
            <template slot-scope="{ row }">
              <img
                :src="
                  'http://localhost:5000/upload/imgs/goods_img/' + row.imgUrl
                "
                alt=""
              /> </template
          ></el-table-column>
          <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button type="danger" @click="update(row)">编辑</el-button>
              <el-button type="default" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="fromObj.currentPage"
            :page-sizes="[3, 6, 9, 12]"
            :page-size="fromObj.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>

          <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="60%"
            :before-close="handleClose"
          >
            <el-form :model="searchObj">
              <el-form-item label="商品名称">
                <el-row>
                  <el-col :span="6">
                    <el-input
                      type="text"
                      placeholder="商品名称"
                      v-model="searchObj.name"
                    ></el-input
                  ></el-col>
                </el-row>
              </el-form-item>

              <el-form-item label="商品分类">
                <el-select
                  placeholder="请选择商品分类"
                  v-model="searchObj.category"
                >
                  <el-option
                    v-for="(item, index) in dataSouce"
                    :key="index"
                    :value="item.cateName"
                    :label="item.cateName"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="商品价格">
                <el-input-number
                  v-model="searchObj.price"
                  :min="1"
                  :max="10"
                  label="描述文字"
                ></el-input-number>
              </el-form-item>

              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:5000/goods/goods_img_upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>

              <el-form-item label="商品描述">
                <el-row>
                  <el-col :span="6">
                    <div style="margin: 20px 0"></div>
                    <el-input
                      type="textarea"
                      placeholder="请输入内容"
                      v-model="searchObj.goodsDesc"
                      maxlength="30"
                      show-word-limit
                    >
                    </el-input>
                  </el-col>
                </el-row>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="add()">添加商品</el-button>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="dialogVisible = false"
                >确 定</el-button
              >
            </span>
          </el-dialog>
        </el-row>
      </template>
    </div></comcard
  >
</template>




<script>
import { api_list, api_dele, api_update } from "../../api/goods.js";
import comcard from "../../components/comcard/index.vue";
export default {
  components: {
    comcard,
  },

  data() {
    return {
      searchObj: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
      dialogVisible: false,
      tableData: [],

      total: 0,

      fromObj: {
        currentPage: 1,
        pageSize: 3,
        name: "",
        category: "",
      },
      searchObj: {},
      dataSouce: [],
      imageUrl: "",
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    //编辑
    async update(row) {
      this.searchObj = row;
      this.dialogVisible = true;
      this.imageUrl = row.imgUrl;
    },
    //渲染
    async query() {
      let rst = await api_list(this.fromObj);
      console.log(rst);
      let { data, total } = rst;
      this.dataSouce = data;
      this.total = total;
    },
    //删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        let rst = await api_dele({ id });
        console.log(rst);
        this.query();
      });
    },
    //分页
    handleSizeChange(val) {
      this.fromObj.pageSize = val;
      this.query();
    },
    handleCurrentChange(val) {
      this.fromObj.currentPage = val;
      this.query();
    },

    handleAvatarSuccess(res, file) {
      let { imgUrl } = res;
      this.imageUrl = `http://localhost:5000/upload/imgs/goods_img/${res.imgUrl}`;
      this.searchObj.imgUrl = imgUrl;
      console.log(res, file);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>